<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/5
 * Time: 18:32
 */

namespace App\Library;


use App\Enums\SysKeyEnums;

class Auth
{

    private static $_instance;
    protected $option;

    public function __construct($option = [])
    {
        $this->option = [
            'access_key' => SysKeyEnums::ACCESS_KEY,
            'secret_key' => SysKeyEnums::SECRET_KEY
        ];
    }

    public static function getInstance($option = [])
    {
        if( null == self::$_instance )
        {
            self::$_instance = new Auth($option);
        }
        return self::$_instance;
    }

    public function params()
    {
        ksort($this->option);
        $sign = '';
        foreach ( $this->option as $k=>$v ){
            if( $v ){
                $sign .= $k."={$v}&";
            }
        }
        if( $sign ){
            $sign = substr($sign, 0, -1);
        }
        $sign = strtolower(md5($sign));
        return array_merge($this->option,['sign'=>$sign]);
    }

    public function header($option)
    {
        $param = array_merge($option,$this->option);
        $json_str = $param['secret_key'].'|'.$param['api_time'].'|'.$param['path'];
        $api_sign = strtoupper(md5($json_str));
        return [
            'Content-Type'      => 'application/json;charset=utf-8',
            'Accept-ApiAccess'  => $param['access_key'],
            'Accept-ClientTime' => $param['api_time'],
            'Accept-ApiSign'    => $api_sign
        ];
    }
}